﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Awesome.Extensions.Linq
{
    public static class LinqEx
    {
        public static IEnumerable<T> Yield<T>(IEnumerable<T> source, T element)
        {
            return source.Concat(element.Yield());
        }

        public static IEnumerable<T> Yield<T>(this T @object)
        {
            yield return @object;
        }

        public static void Apply<T>(IEnumerable<T> source, Action<T> action)
        {
            foreach (var element in source)
            {
                action(element);
            }
        }

        public static IEnumerable<T> Eval<T>(IEnumerable<T> source)
        {
            return source.ToList();
        }

        public static void Enumerate<T>(this IEnumerable<T> source)
        {
            foreach (var _ in source)
            {
                /* nothing */
            }
        }

        public static int CountOf<T>(this IEnumerable<T> source, T value)   
        {
            return source.Count(f => f.Equals(value));
        }

        public static bool HasOnlyOne<T>(this IEnumerable<T> source, T value)
        {
            return source.CountOf(value) == 1;
        }

        public static bool IsEmpty<T>(this IEnumerable<T> source)
        {
            return !source.Any();
        }
    }
}
